import api from './apis/api-patrol.js'
import ui from './ui.js'
/**
 * 上传移动巡检记录
 */
function uploadMobileRecord(record, success) {
	// 定义一个有附件的danger集合
	this.index = 0;
	this.attachmentList = [];
	// 找出有附件的danger放入集合中
	for (let i = 0; i < record.items.length; i++) {
		let danger = record.items[i];
		if (danger.attachmentLocalList.length > 0) {
			for (let j = 0; j < danger.attachmentLocalList.length; j++) {
				let doc = danger.attachmentLocalList[j];
				this.attachmentList.push({
					path: doc.path,
					dangerId: danger.dangerId,
					employeeName: record.patrolEmployeeName
				})
			}
		}
	}

	// 定义两个方法
	// 上传附件
	this.uploadFile = function() {
		let attachment = this.attachmentList[this.index];
		api.uploadPatrolFile(attachment.dangerId, attachment.employeeName, attachment.path)
			.then(res => {

			})
			.finally(() => {
				// 继续上传
				this.index++;
				if (this.index >= this.attachmentList.length) {
					// 说明上传完了 
					this.uploadMobileJson();
				} else {
					// 未上传完成，继续上传
					this.uploadFile();
				}
			});
	}


	/**
	 * 上传json
	 */
	this.uploadMobileJson = function() {
		api.uploadMobileRecord(record)
			.then(res => {
				success(res)
			})
			.finally(() => {
				ui.hideLoading()
			});
	}

	ui.showLoading()
	if (this.attachmentList.length == 0) {
		this.uploadMobileJson()
	} else {
		this.uploadFile()
	}

}

/**
 * 上传设备记录
 * @param {Object} record
 * @param {Object} success
 */
function uploadDeviceRecord(record,success){
	// 定义一个有附件的danger集合
	this.index = 0;
	this.attachmentList = [];
	// 找出有附件的danger放入集合中
	for (let i = 0; i < record.contents.length; i++) {
		let danger = record.contents[i];
		if (danger.attachmentLocalList.length > 0) {
			for (let j = 0; j < danger.attachmentLocalList.length; j++) {
				let doc = danger.attachmentLocalList[j];
				this.attachmentList.push({
					path: doc.path,
					dangerId: danger.dangerId,
					employeeName: record.patrolEmployeeName
				})
			}
		}
	}
	// 定义两个方法
	// 上传附件
	this.uploadFile = function() {
		let attachment = this.attachmentList[this.index];
		api.uploadPatrolFile(attachment.dangerId, attachment.employeeName, attachment.path)
			.then(res => {
	
			})
			.finally(() => {
				// 继续上传
				this.index++;
				if (this.index >= this.attachmentList.length) {
					// 说明上传完了 
					this.uploadDeviceJson();
				} else {
					// 未上传完成，继续上传
					this.uploadFile();
				}
			});
	}
	/**
	 * 上传json
	 */
	this.uploadDeviceJson = function() {
		api.uploadDeviceRecord(record)
			.then(res => {
				success(res)
			})
			.finally(() => {
				ui.hideLoading()
			});
	}
	
	ui.showLoading()
	if (this.attachmentList.length == 0) {
		this.uploadDeviceJson()
	} else {
		this.uploadFile()
	}
}

/**
 * 坐标点集合转为string
 */
function latlngListToString(list) {
	if (list == null || list.length == 0) {
		return ""
	};
	let s = '';
	for (let i = 0; i < list.length; i++) {
		let latLng = list[i];
		s += (latLng.latitude + ' ' + latLng.longitude)
		if (i != list.length - 1) {
			s += ','
		}
	}
	return s;
}



export default {
	uploadMobileRecord,
	uploadDeviceRecord,
	latlngListToString
}
